package com.cxd.base.page;

import javax.servlet.http.HttpServletRequest;

import com.cxd.base.page.tag.TagPage;
import com.cxd.base.util.wtj.SUtils;

public class PageUtil {
	/**
	 * @author wlsnow
	 * @date 2014-8-21 下午9:29:37
	 * @param request
	 * @return
	 * @see [类、类#方法、类#成员]
	 */
	public static  TagPage getTagPage(HttpServletRequest request){
		TagPage page = new TagPage();
		int currentPage = request.getParameter("currentPage")==null?1:Integer.parseInt(request.getParameter("currentPage"));
		int showCount = request.getParameter("showCount")==null?5:Integer.parseInt(request.getParameter("showCount"));
		if (currentPage<=0){
			currentPage =1;
		}
		int currentResult = (currentPage-1) * showCount;
		page.setShowCount(showCount);
		page.setCurrentResult(currentResult);
		page.setCurrentPage(currentPage);
		return page;
	} 
	
	public static  TagPage getTagPage(String currentPage,String pageSize){
		TagPage page = new TagPage();
		int currentPageInt = SUtils.isEmpty(currentPage)?1:Integer.parseInt(currentPage);
		int pageSizeInt = SUtils.isEmpty(pageSize)?5:Integer.parseInt(pageSize);
		if (currentPageInt<=0){
			currentPageInt =1;
		}
		int currentResult = (currentPageInt-1) * pageSizeInt;
		page.setShowCount(pageSizeInt);
		page.setCurrentResult(currentResult);
		page.setCurrentPage(currentPageInt);
		return page;
	} 
	
	/**
	 * 
	 * <一句话功能简述> <功能详细描述>
	 * 
	 * @author wlsnow
	 * @date 2014-8-21 下午9:29:31
	 * @param request
	 * @param page
	 * @return
	 * @see [类、类#方法、类#成员]
	 */

public static  String getPageStr(HttpServletRequest request,TagPage page){
	int lastPage=0; // 最大页
	int totalCount = page.getTotalResult();  //总记录数
	int showCount = page.getShowCount();  // 每页显示多少
	int currentPage = page.getCurrentPage(); // 当前页
	String  reqUri = request.getRequestURI();
	
	if (totalCount % showCount==0){
		lastPage = totalCount / showCount;
	}else{
		lastPage =1+ totalCount / showCount;
	}
	
	if (currentPage>=lastPage){
		currentPage =lastPage;
	}

	int startNo = 1;
	int endNo 	= 1;
	// 显示按钮个数
	int showButCount = 9;
	int cha = (showButCount-1)/2;
	if(lastPage > showButCount){
		if(currentPage  < cha){
			endNo = showButCount;
		}else{
			if(currentPage+cha > lastPage){
				startNo = lastPage-showButCount+1;
				endNo   = lastPage;
			}else{
				startNo = currentPage-cha;
				endNo   = currentPage+cha;
			}
		}
	}else{
		endNo = lastPage;
	}
	
	
	StringBuffer sb = new StringBuffer();
	int pageno 	= currentPage; // 当前页
	int pagecount 	= lastPage;// 总页数
	
	sb.append("<div class='pagin'>");
	sb.append("	<div class='message'>共<i class='blue'>"+totalCount+"</i>条记录，当前显示第&nbsp;<i class='blue'>"+pageno+"&nbsp;</i>页</div>");
	sb.append("    <ul class='paginList'>");
	// 上一页
	sb.append("	    <li class='paginItem'><a href='"+buildPagerUrl7(page, (pageno<2?1:pageno- 1), "")+"'><span class='pagepre'></span></a></li>");
	
	
	for (int i = startNo; i <= endNo; i++) {
		String current = "";
		if(pageno == i){
			current = "current";
		}
		StringBuffer strurl = new StringBuffer();
		strurl.append(reqUri).append("?");
		if (page.getQueryString() == null || page.getQueryString() == "") {
			strurl.append("page=").append(i);
		} else {
			strurl.append(page.getQueryString()).append("&page=").append(i);
		}
		
		sb.append("<li class='paginItem "+current+"'><a href='"+strurl+"'>"+i+"</a></li>");
	}
	// 下一页
	sb.append("	    <li class='paginItem'><a href='"+buildPagerUrl7(page, pageno + 1>pagecount?pagecount:pageno + 1, reqUri)+"'><span class='pagenxt'></span></a></li>");
	sb.append("    </ul>");
	sb.append("</div>");
    
	
	return sb.toString();
} 
	
	/**
	 * @author wlsnow
	 * @date 2014-8-21 下午9:29:25
	 * @param page
	 * @param pageNo
	 * @param baseUrl
	 * @return
	 * @see [类、类#方法、类#成员]
	 */
	private static  String buildPagerUrl7(TagPage page, int pageNo,String baseUrl) {
		StringBuffer sb = new StringBuffer();
		sb.append(baseUrl).append("?");
		if (page.getQueryString() == null || page.getQueryString() == "") {
			sb.append("page=").append(pageNo);
		} else {
			sb.append(page.getQueryString()).append("&page=").append(pageNo);
		}
		return sb.toString();
	}
}
